	
	google.load("visualization", "1", {packages:["corechart"]});
	
	//Define the map, map style, map options, the location list, and a marker array
	
	var locations = [];
	var map;
	var styles = [
			{
				featureType: "road.local",
				stylers: [ { visibility: "on" } ]
			},
			{
				featureType: "road.highway",
				stylers: [ { visibility: "off" } ]
			},
			{
				featureType: "road.arterial",
				stylers: [ { visibility: "simplified" } ]
			},
			{
				featureType: "administrative.locality",
				stylers: [ { visibility: "simplified" } ]
			},
			{
				featureType: "administrative.country",
				stylers: [ { visibility: "simplified" } ]
			},			
	];
	var myLatlng = new google.maps.LatLng(32.25, 35);
	var mapOptions = {
	  center: myLatlng,
	  zoom: 9,
	  mapTypeId: google.maps.MapTypeId.ROADMAP,
			  styles: styles,
			  mapTypeControl: false,
			  streetViewControl: false
	};
	var markersArray = [];
	
	//Define in a SINGLE place the cause color
	var Acolor = 'purple.png';// #835C99
	var Ccolor = 'orange.png';// #F57640 
	var Ecolor = 'red.png';   // #CF5D5D
	var Fcolor = 'blue.png';  // #448C9C
	var Mcolor = 'green.png'; // #679E73
	var Wcolor = 'yellow.png';// #F5D311
	
	
	
	//FUNCTION: Initial setting up of the map and the location list
	
	function initialize() {
		map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
		locations = readCSV("locations.csv");
	}
	
	
	
	
	//FUNCTION: Readind CSV file into var locations
	
	function readCSV(url) {
		var location = [];
		var locationList = [];
		var csvFile = new XMLHttpRequest();
		csvFile.open("POST",url,false);
		csvFile.send();
		var csvArr = csvFile.responseText.split("\n");
		for (var i = 0; i < csvArr.length - 1; i++) {
			var tempArr = csvArr[i].split(",");
			location = [];
			location["title"] = tempArr[0];
			location["lat"] = tempArr[1];
			location["lng"] = tempArr[2];
			location["cause"] = tempArr[3];
			location["date"] = tempArr[4];
			locationList[i] = location;
		}
		return locationList;
	}
	
	
	
//FUNCTION: Setting the markers according to form data
	
	function setMarkers() {

	//Define variables
		var resultsFound = 0;
		var causeList = '';
		var infobox = new InfoBox();

	// Get cause list
		var causeArray = [];
		causeArray = getCause();

	// Get begin and end dates, and the village date coming from the csv
		var villageDate = new Date();
		var beginDate = new Date();
		var endDate = new Date();
		beginDate = document.getElementById("begin-date").value;
		endDate = document.getElementById("end-date").value;
		var accurateCheck = false;
		if (document.getElementById("accuracy").checked == true) {
			accurateCheck = true;
		}
		
	//Clear all existing markers
		if (markersArray) {
				for (var m in markersArray) {
					markersArray[m].setMap(null);
				}
		}
		
		markersArray = [];
		


	// Put the markers in position with proper attributes
		for (var i = 0; i < locations.length; i++) {
			var matchFound = false;
			var noMatchesFound = 0;
			var Village = [];
			Village = locations[i];
			villageDate = Village.date;
			villageCauses = Village.cause.split(":");
			var VLatLng = new google.maps.LatLng(Village.lat, Village.lng);
			if (villageDate >= beginDate && villageDate <= endDate) {
				for (var c = 0; c < villageCauses.length; c++) {
					for (var e = 0; e < causeArray.length; e++) {
						if (causeArray[e] == villageCauses[c]) {
							noMatchesFound += 1;
							if ((accurateCheck == false) || ((accurateCheck == true) && (noMatchesFound == villageCauses.length) && (noMatchesFound == causeArray.length))) {
								markerIcon = markerColor(villageCauses[c]);
								var marker = new google.maps.Marker({
										position: VLatLng,
										map: map,
										title: Village.title,
										icon: markerIcon
								});
								markersArray.push(marker);
								// Bind infowindows to their markers
								var description = document.createElement('div');
								description.style.cssText = "background-color: #FEFFFD; opacity: 0.8; padding: 10px; border: 1px solid grey; border-radius: 6px; width: 210px;";
								var causeText = assignDescription(villageCauses);
								description.innerHTML = "<font face='Palatino Linotype'><h3>" + Village.title + "</h3>" + Village.date + "<br><br>" + causeText;
								bindInfo(marker, map, infobox, description);
								matchFound = true;
								break;
							}
						}
					}
					if  (matchFound == true) {
						resultsFound += 1;
						for (var l = 0; l < villageCauses.length; l++){
							causeList += villageCauses[l] + ',';
						}
						break;
					}
				}
			}
		}
		addStats (resultsFound, causeArray, causeList);
	}
	

	//FUNCTION: Set up the Info windows
	
	function bindInfo(marker, map, infobox, description) {
		google.maps.event.addListener(marker, 'mouseover', function() {
			infobox.setOptions({
			 content: description
			 ,disableAutoPan: false
			 ,closeBoxURL: ""
			});
			infobox.open(map, marker);
		});
		google.maps.event.addListener(marker, 'mouseout', function() {
			infobox.close();
		});
	}

	
	
	//FUNCTION: Get the selected causes from the form
	function getCause() {
		var causeArray = [];
		var chkBoxes = document.getElementsByName("cause");
		for (var i = 0; i < chkBoxes.length; i++) {
			if (chkBoxes[i].checked == true) {
				causeArray.push(chkBoxes[i].value);
			}
		}
		return causeArray;
	}
	
	//FUNCTION: assign the correct cause description for each village's infowindow
	function assignDescription(villageCauses) {
		var text = "";
		for (var c = 0; c < villageCauses.length; c++) {
			if (villageCauses[c] == "A") {
				text += "Abandonment on Arab Orders<br>";
			}
			else if (villageCauses[c] == "C") {
				text += "Influence of nearby town's fall<br>";
			}
			else if (villageCauses[c] == "E") {
				text += "Expulsion by Jewish forces<br>";
			}
			else if (villageCauses[c] == "F") {
				text += "Fear of being caught up in fighting<br>";
			}
			else if (villageCauses[c] == "M") {
				text += "Military assault on settlement<br>";
			}
			else if (villageCauses[c] == "W") {
				text += "Whispering campaigns - psychological warfare<br>";
			}
		}
		return text;
	}
	
	//FUNCTION: set the proper wave dates in the date boxes
	function setWave() {
		var wave = document.getElementById("wave-selectbox").value;
		if (wave == "First Wave") {
			document.getElementById("begin-date").value = "1947-11-29";
			document.getElementById("end-date").value = "1948-03-31";
		} else if (wave =="Second Wave") {
			document.getElementById("begin-date").value = "1948-04-01";
			document.getElementById("end-date").value = "1948-06-30";
		} else if (wave == "Third Wave") {
			document.getElementById("begin-date").value = "1948-07-09";
			document.getElementById("end-date").value = "1948-07-18";
		} else if (wave == "Fourth Wave") {
			document.getElementById("begin-date").value = "1948-10-15";
			document.getElementById("end-date").value = "1948-10-31";
		} else if (wave == "Post Nov 1948") {
			document.getElementById("begin-date").value = "1948-11-01";
			document.getElementById("end-date").value = "1949-07-20";
		} else if (wave == "1947-1949") {
			document.getElementById("begin-date").value = "1947-11-29";
			document.getElementById("end-date").value = "1949-07-20";
		}
	}
	
	//FUNCTION: assign marker icon
	function markerColor(cause) {
		var markerIcon;
		if (cause == "A") {
			markerIcon = Acolor;
		}
		else if (cause == "C") {
			markerIcon = Ccolor;
		}
		else if (cause == "E") {
			markerIcon = Ecolor;
		}
		else if (cause == "F") {
			markerIcon = Fcolor;
		}
		else if (cause == "M") {
			markerIcon = Mcolor;
		}
		else if (cause == "W") {
			markerIcon = Wcolor;
		}
		return markerIcon;
	}
	
	//FUNCTION: add result stats
	function addStats (resultsFound, causeArray, causeList) {
		var container = document.getElementById('container');
		var statsDiv = document.getElementById('statsDiv');
		if (statsDiv == null) {
			statsDiv = document.createElement('div');
			statsDiv.setAttribute('id','statsDiv');
			container.appendChild(statsDiv);
			var resultSum = document.createElement('div');
			resultSum.setAttribute('id','resultSum');
		
			statsDiv.appendChild(resultSum);
			// var colorIndex = document.createElement('div');
			// colorIndex.setAttribute('id','colorIndex');
			// charts.appendChild(colorIndex);
			var chart = document.createElement('div');
			chart.setAttribute('id','chartDiv');
			statsDiv.appendChild(chart);
		} else {
			var resultSum = document.getElementById('resultSum');
			// var colorIndex = document.getElementById('colorIndex');
			var chart = document.getElementById('chartDiv');
		}
		// var indexContent = setIndexContent(causeArray);
		resultSum.innerHTML = '<p>' + resultsFound + ' results found</p>';
		// colorIndex.innerHTML = indexContent;
		drawChart(causeList);
	}
	
	//FUNCTION: draw distribution chart
	function drawChart(causeList) {
		var A = causeList.split("A").length - 1;
		var C = causeList.split("C").length - 1;
		var E = causeList.split("E").length - 1;
		var F = causeList.split("F").length - 1;
		var M = causeList.split("M").length - 1;
		var W = causeList.split("W").length - 1;
			var data = google.visualization.arrayToDataTable([
			  ['Cause', 'Occurences'],
			  ['Abadonment',     A],
			  ['Influence',      C],
			  ['Expulsion',      E],
			  ['Fear',           F],
			  ['Military',       M],
			  ['Psych Warfare',  W]
			]);

			var options = {
			  colors:['#835C99','#F57640','#CF5D5D','#448C9C','#679E73','#F5D311']
			};

			var chart = new google.visualization.PieChart(document.getElementById('chartDiv'));
			chart.draw(data, options);
	}
	
	
	
	//FUNCTION: determine index content
	function setIndexContent (causeArray) {
		var indexContent = '';
		for (var i = 0; i < causeArray.length; i++) {
			if (causeArray[i] == "A") {
				indexContent += '<img src="purple.png" height="16" width="10"> = Abandonment   <br>'
			} else if (causeArray[i] == "C") {
				indexContent += '<img src="orange.png" height="16" width="10"> = Influence   <br>'
			} else if (causeArray[i] == "E") {
				indexContent += '<img src="red.png" height="16" width="10"> = Expulsion   <br>'
			} else if (causeArray[i] == "F") {
				indexContent += '<img src="blue.png" height="16" width="10"> = Fear   <br>'
			} else if (causeArray[i] == "M") {
				indexContent += '<img src="green.png" height="16" width="10"> = Military   <br>'
			} else if (causeArray[i] == "W") {
				indexContent += '<img src="yellow.png" height="16" width="10"> = Psych Warfare   <br>'
			}
		}
		return indexContent;
	}
	
	function showAbout() {
		var container = document.getElementById('container');
		var about = document.getElementById('aboutText');
		if (about == null) {
			about = document.createElement('div');
			about.setAttribute('id','aboutText');
			about.innerHTML = "<B>BoPRP</B> is a web version of the second map featured in Benny Morris' <i>Birth of the Palestinian Refugee Problem</i>. It is based upon a comprehensive list of the majority of the Arab settlements depopulated during the 1948 Arab-Israeli war, including the date of the event and the main cause for it. <br>The aim of the map is to give a broad view of that particular aspect of the war, to describe the four waves of both forced and voluntary migration as described by Morris, and to analyze the impact of each of the causes of that migration.";
			container.appendChild(about);
			
		} else {
			container.removeChild(about);
		}
	}
	